package com.bobochang.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobochang.warehouse.entity.Result;
import com.bobochang.warehouse.entity.WorkRegion;
import com.bobochang.warehouse.page.Page;
import com.bobochang.warehouse.service.WorkRegionService;
import com.bobochang.warehouse.mapper.WorkRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author magic'book
* @description 针对表【work_region】的数据库操作Service实现
* @createDate 2023-12-12 10:42:16
*/
@Service
public class WorkRegionServiceImpl extends ServiceImpl<WorkRegionMapper, WorkRegion>
    implements WorkRegionService{
    @Autowired
    private WorkRegionMapper workRegionMapper;

    @Autowired
    private DepartmentServiceImpl departmentService;

    @Override
    public List<WorkRegion> queryAllWorkRegion(Integer departmentId) {
        return baseMapper.selectList(new LambdaQueryWrapper<WorkRegion>()
                    .eq(WorkRegion::getStatus, 0)
                    .eq(departmentId != null && departmentId != 0, WorkRegion::getDepartmentId, departmentId));
    }

    @Override
    public Result searchById(Integer workregionId) {
        LambdaQueryWrapper<WorkRegion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkRegion::getId, workregionId);
        return Result.ok(baseMapper.selectOne(queryWrapper));
    }

    @Override
    public Page queryDepartmentPage(Page page, WorkRegion workRegion) {
        return null;
    }

    @Override
    public Result saveDeparment(WorkRegion workRegion) {
        return null;
    }

    @Override
    public Result updateDepartment(WorkRegion workRegion) {
        return null;
    }

    @Override
    public void deleteWorkregionById(Integer workregionId) {
        WorkRegion workRegion = baseMapper.selectOne(new LambdaQueryWrapper<WorkRegion>().eq(WorkRegion::getId, workregionId));
        if(workRegion != null){
            workRegion.setStatus(1);
            baseMapper.updateById(workRegion);
        }
    }

    @Override
    public Result updateWorkregion(WorkRegion workRegion) {
        if(baseMapper.updateById(workRegion) > 0){
            return Result.ok("成功");
        }
        else {
            return Result.err(Result.CODE_ERR_BUSINESS, "失败");
        }
    }

    @Override
    public Result saveWorkregion(WorkRegion workRegion) {
        if(baseMapper.selectCount(new LambdaQueryWrapper<WorkRegion>()
                .eq(WorkRegion::getRegionName, workRegion.getRegionName())) > 0){
            return Result.err(Result.CODE_ERR_BUSINESS,"工区名已存在");
        }else {
            if(baseMapper.insert(workRegion) > 0){
                return Result.ok("成功");
            }else {
                return Result.err(Result.CODE_ERR_BUSINESS, "添加失败");
            }
        }
    }

    @Override
    public Page queryWorkregionPage(Page page, WorkRegion workRegion) {
        //查询工区总行数
        int userCount = baseMapper.selectCount(
                new LambdaQueryWrapper<WorkRegion>()
                        .eq(WorkRegion::getStatus, 0)
                        .like(workRegion.getRegionName() != null, WorkRegion::getRegionName, workRegion.getRegionName())
                        .eq(workRegion.getDepartmentId() != null, WorkRegion::getDepartmentId, workRegion.getDepartmentId())
        );
        //分页查询工区
        List<WorkRegion> userList = baseMapper.selectList(
                new LambdaQueryWrapper<WorkRegion>()
                .eq(WorkRegion::getStatus, 0)
                .like(workRegion.getRegionName() != null, WorkRegion::getRegionName, workRegion.getRegionName())
                .eq(workRegion.getDepartmentId() != null, WorkRegion::getDepartmentId, workRegion.getDepartmentId())
                .last("limit "+ page.getLimitIndex() + "," + page.getPageSize())
                );

        userList.stream()
                .filter(Objects::nonNull)
                .forEach(u -> {
                    u.setDepartmentName(departmentService.findById(u.getDepartmentId()));
                });

        //将查询到的总行数和当前页数据组装到Page对象
        page.setTotalNum(userCount);
        page.setResultList(userList);

        return page;
    }
}




